//
//  ReadDetailViewController.m
//  A_Project_DLS151129_SQ
//
//  Created by 151129-49宋强 on 16/1/20.
//  Copyright © 2016年 www.lanou3g.com蓝鸥. All rights reserved.
//
#import "ReadDetailViewController.h"
#import "ReadDetailCollectionViewCell.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height

@interface ReadDetailViewController ()< UICollectionViewDataSource, UICollectionViewDelegate, UIScrollViewDelegate, jumpDelegata, refreshDelegate, loadingDelegate>
@property (nonatomic, retain)UIScrollView *xianScrollView;
@property (nonatomic, retain)NSMutableArray *buttonArr;
@property (nonatomic, retain)UICollectionView *collectionView;
@property (nonatomic, retain)NSMutableArray *dataArr;
@property (nonatomic, retain)NSMutableArray *dataArrTwo;
@property (nonatomic, assign)NSInteger i;
@end

@implementation ReadDetailViewController
- (void)dealloc
{
    [_xianScrollView release];
    [_buttonArr release];
    [_collectionView release];
    [_dataArr release];
    [_dataArrTwo release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = self.title1;
    //创建button
    [self creatDoubleButton];
    //创建collectionView
    [self creatCollectionView];
    //数据处理
    [self dataHandle];
    self.i = 0;
}
//刷新协议方法
- (void)refresh
{
    //清空数组
    self.dataArr = nil;
    self.dataArrTwo = nil;
    self.i = 0;
    //刷新数据
    [self dataHandle];
}
//加载协议方法
- (void)loading
{
    
    self.i += 10;
    NSString *urlStr = @"http://api2.pianke.me/read/columns_detail";
    NSDictionary *dic = @{@"sort":@"addtime", @"start": @(_i), @"client": @2, @"typeid": @(self.tid), @"limit":@10};
    [PWNetWorking POSTWithURL:urlStr body:dic completion:^(id result) {
        NSArray *arr = result[@"data"][@"list"];
       NSMutableArray *addArr = [Model modelHanderWithArray:arr];
        for (Model *m in addArr) {
        [self.dataArr addObject:m];
        }
        [_collectionView reloadData];
    }];
    NSMutableDictionary *dic1 = [dic mutableCopy];
    [dic1 setObject:@"hot" forKey:@"sort"];
    [PWNetWorking POSTWithURL:urlStr body:dic1 completion:^(id result) {
        NSArray *arr = result[@"data"][@"list"];
        NSMutableArray *addArr = [Model modelHanderWithArray:arr];
        for (Model *m in addArr) {
            [self.dataArrTwo addObject:m];
        }
        [_collectionView reloadData];
    }];

}
- (void)dataHandle
{
    NSString *urlStr = @"http://api2.pianke.me/read/columns_detail";
    NSDictionary *dic = @{@"sort":@"addtime", @"start": @0, @"client": @2, @"typeid": @(self.tid), @"limit":@10};
    [PWNetWorking POSTWithURL:urlStr body:dic completion:^(id result) {
        NSArray *arr = result[@"data"][@"list"];
        self.dataArr = [Model modelHanderWithArray:arr];
        [_collectionView reloadData];
    }];
    NSMutableDictionary *dic1 = [dic mutableCopy];
    [dic1 setObject:@"hot" forKey:@"sort"];
    [PWNetWorking POSTWithURL:urlStr body:dic1 completion:^(id result) {
        NSArray *arr = result[@"data"][@"list"];
        self.dataArrTwo = [Model modelHanderWithArray:arr];
        [_collectionView reloadData];
    }];
    
}
#pragma mark - 协议方法 跳转页面
- (void)jump:(NSString *)str Model:(Model *)model
{
    ReadWebVC *webVC = [[ReadWebVC alloc] init];
    [self.navigationController pushViewController:webVC animated:YES];
    webVC.contenId = str;
    webVC.collectionModel = model;
    [webVC release];
}
//创建collectionView
- (void)creatCollectionView
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    layout.itemSize = CGSizeMake(WIDTH, HEIGHT - 50 - 64);
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 51, WIDTH, HEIGHT - 50 - 64) collectionViewLayout:layout];
    
    self.collectionView.contentSize = CGSizeMake(WIDTH *2, 0);
    self.collectionView.pagingEnabled = YES;
    _collectionView.bounces = NO;
    
    self.collectionView.backgroundColor = [UIColor yellowColor];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    [self.view addSubview:self.collectionView];
    [layout release];
    [self.collectionView release];
    [self.collectionView registerClass:[ReadDetailCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    
    
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 2;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ReadDetailCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    if (indexPath.item == 0) {
        cell.dataArr = _dataArr;
    }else{
        cell.dataArr = _dataArrTwo;
    }
    cell.delegate = self;
    cell.reDelegate = self;
    cell.loDelegate = self;
    return cell;
}
#pragma mark - 创建最新最热两个btn
- (void)creatDoubleButton
{
    NSArray *arr = @[@"最新", @"最热"];
    self.buttonArr = [NSMutableArray array];
    for (NSInteger i = 0; i < 2; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
        btn.frame = CGRectMake(WIDTH / 2 * i, 0, WIDTH / 2, 49);
        [btn setTitle:arr[i] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        btn.tag = 1 + i;
        [btn addTarget:self action:@selector(btn:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:btn];
        [self.buttonArr addObject:btn];
    }
    [_buttonArr[0] setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    
    self.xianScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 49, WIDTH, 1)];
    _xianScrollView.contentSize = CGSizeMake(WIDTH, 1);
    [self.view addSubview:_xianScrollView];
    [_xianScrollView release];
    UIView *qq = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH / 2, 1)];
    qq.backgroundColor = [UIColor redColor];
    [self.xianScrollView addSubview:qq];
}
#pragma mark - 点击btn与scrollerView联动
- (void)btn:(UIButton *)button
{
    for (UIButton *btn in _buttonArr) {
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    }
    [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    
    [_xianScrollView setContentOffset:CGPointMake(- WIDTH / 2 * (button.tag - 1), 0) animated:YES];
    [_collectionView setContentOffset:CGPointMake(WIDTH * (button.tag - 1), 0) animated:YES];
}
#pragma mark - 滑动与btn联动
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (scrollView == _collectionView) {
        
        [_xianScrollView setContentOffset:CGPointMake(- scrollView.contentOffset.x / 2, 0) animated:YES];
    }
    NSInteger i = scrollView.contentOffset.x / WIDTH;
    for (UIButton *btn in _buttonArr) {
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    }
    [_buttonArr[i] setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    
    
}
- (void)viewWillAppear:(BOOL)animated
{
    for (UIButton *btn in _buttonArr) {
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    }
    [_buttonArr[0] setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    _collectionView .contentOffset = CGPointZero;
    _xianScrollView.contentOffset = CGPointZero;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
